//轮播图片上传
$('#file').on('change', function () {
    let formData = new FormData()
    formData.append('image', this.files[0])
    $.ajax({
        type: 'post',
        url: '/upload',
        data: formData,
        processData: false,
        contentType: false,
        success: function (response) {
            $('#image').val(response[0].image)
        }
    })
})

//提交添加轮播图表单
$('#slideForm').on('submit', function () {
    let formData = $(this).serialize()
    $.ajax({
        type: 'post',
        url: '/slides',
        data: formData,
        success: function () {
            location.reload()
        }
    })
    return false
})

$.get('/slides', function (response) {
    console.log(response);
    let html = template('slidesTpl', { data: response })
    $('tbody').html(html)
})

//删除轮播图
$('tbody').on('click', '.delete', function () {
    if (confirm('确定删除这个用户吗?')) {
        let id = $(this).attr('data-id')
        //发起删除的请求
        $.ajax({
            type: 'delete',
            url: '/slides/' + id,
            success: function () {
                //重新加载页面
                location.reload()
            }
        })
    }
})